import App from './App'

// #ifndef VUE3
import Vue from 'vue'
import './uni.promisify.adaptor'
Vue.config.productionTip = false
App.mpType = 'app'
const app = new Vue({
	...App
})
app.$mount()
// #endif

// #ifdef VUE3
import {
	createSSRApp
} from 'vue'
export function createApp() {
	const app = createSSRApp(App)
	return {
		app
	}
}
// #endif

// main.js，注意要在use方法之后执行
import uView from 'uview-ui'
Vue.use(uView)
// 如此配置即可
uni.$u.config.unit = 'rpx'

let base = "http://localhost:8080/dfm-driver-bus"
// let base = "https://4391945439.goho.co/dfm-driver-bus"

Vue.prototype.urls = {
	registerDriver: `${base}/driver/registerDriver`,
	uploadPrivate: `${base}/oss/upload`,
	updateDriverAuth: `${base}/driver/updateDriverAuth`,
	createDriverFace: `${base}/driver/createDriverFace`,
	login: `${base}/driver/login`,
	searchDriverBaseInfo: `${base}/driver/searchDriverBaseInfo`,
	logout: `${base}/driver/logout`,
	updateOrderInfo: `${base}/driver/order/update`,
}

// 统一请求封装
Vue.prototype.request = function(url, method, data, successCallback, errorCallback, showLoading = true) {
	if (showLoading) {
		uni.showLoading({
			title: "请求处理中......"
		});
	}
	uni.request({
		"url": url,
		"method": method,
		"header": {
			token: uni.getStorageSync("token")
		},
		"data": data,
		success: function(resp) {
			console.log("请求响应:", resp)
			if (showLoading) {
				uni.hideLoading();
			}

			if (resp.statusCode == 401) {
				uni.redirectTo({
					url: "/pages/index/index"
				});
			} else if (resp.statusCode == 200 && resp.data.code == 100) {
				console.log("responseData", responseData);
				const responseData = resp.data;
				// 成功响应处理
				if (responseData.data.hasOwnProperty("token")) {
					const token = responseData.data.token;
					uni.setStorageSync("token", token);
				}
				successCallback(responseData);

			} else {
				// 异常业务处理
				handleError(resp);
				errorCallback(resp);
			}
		},
		fail: function(error) {
			console.log("请求错误 error", error)
			if (showLoading) {
				uni.hideLoading();
			}
			handleError(error);
			errorCallback(error);
		},
		complete(msg) {
			console.log("请求完成 msg", msg);
			if (showLoading) {
				uni.hideLoading();
			}
		}
	});
}

function handleError(resp) {
	let titleTxt = "系统异常";
	// 业务失败异常处理
	if (resp.hasOwnProperty("data") && resp.data.message != null) {
		titleTxt = resp.data.message;
	}
	uni.showToast({
		icon: 'error',
		title: titleTxt,
	});
	console.error(resp);
}

// 统一文件云存储请求
Vue.prototype.uploadCos = function(url, path, fun) {
	uni.uploadFile({
		url: url,
		filePath: path,
		name: "file",
		header: {
			token: uni.getStorageSync("token")
		},
		// formData: {
		// 	"module": module
		// },
		success: function(resp) {
			let result = JSON.parse(resp.data)
			console.log("uploadCos resp", result);
			if (resp.statusCode == 401) {
				uni.redirectTo({
					url: "/pages/index/index"
				})
			} else if (resp.statusCode == 200 && result.code == 100) {
				fun(result.data)
			} else {
				uni.showToast({
					icon: "error",
					title: "系统异常"
				})
			}
		}
	})
}

Vue.prototype.checkNull = function(data, name) {
	if (data == null) {
		this.$refs.uToast.show({
			title: name + "不能为空",
			type: 'error'
		})
		return true
	}
	return false
}

Vue.prototype.checkBlank = function(data, name) {
	if (data == null || data == "") {
		this.$refs.uToast.show({
			title: name + "不能为空",
			type: 'error',
			message: name + "不能为空",
		})
		return true
	}
	return false
}

Vue.prototype.checkValidName = function(data, name) {
	if (data == null || data == "") {
		this.$refs.uToast.show({
			title: name + "不能为空",
			type: 'error',
			message: name + "不能为空"
		})
		return false
	} else if (!/^[\u4e00-\u9fa5]{2,15}$/.test(data)) {
		this.$refs.uToast.show({
			title: name + "不正确",
			type: 'error',
			message: name + "不正确"
		})
		return false
	}
	return true
}

Vue.prototype.checkValidTel = function(data, name) {
	if (data == null || data == "") {
		this.$refs.uToast.show({
			title: name + "不能为空",
			type: 'error',
			message: name + "不能为空"
		})
		return false
	} else if (!/^1[0-9]{10}$/.test(data)) {
		this.$refs.uToast.show({
			title: name + "不正确",
			type: 'error',
			message: name + "不正确"
		})
		return false
	}
	return true
}

Vue.prototype.checkValidEmail = function(data, name) {
	if (data == null || data == "") {
		this.$refs.uToast.show({
			title: name + "不能为空",
			type: 'error',
			message: name + "不能为空"
		})
		return false
	} else if (!/^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/.test(data)) {
		this.$refs.uToast.show({
			title: name + "不正确",
			type: 'error',
			message: name + "不正确"
		})
		return false
	}
	return true
}

Vue.prototype.checkValidAddress = function(data, name) {

	if (data == null || data == "") {
		this.$refs.uToast.show({
			title: name + "不能为空",
			type: 'error',
			message: name + "不能为空"
		})
		return false
	} else if (!/^[0-9a-zA-Z\u4e00-\u9fa5\-]{6,50}$/.test(data)) {
		this.$refs.uToast.show({
			title: name + "不正确",
			type: 'error',
			message: name + "不正确"
		})
		return false
	}
	return true
}

Vue.prototype.checkValidFee = function(data, name) {

	if (data == null || data == "") {
		this.$refs.uToast.show({
			title: name + "不能为空",
			type: 'error',
			message: name + "不能为空"
		})
		return false
	} else if (!/^[1-9]\d*\.\d{1,2}$|^0\.\d{1,2}$|^[1-9]\d*$/.test(data)) {
		this.$refs.uToast.show({
			title: name + "不正确",
			type: 'error',
			message: name + "不正确"
		})
		return false
	}
	return true
}

Vue.prototype.changeNumber = function(value) {
	let newValue = ['', ''];
	let fr = 1000;
	const ad = 1;
	let num = 3;
	const fm = 1;
	while (value / fr >= 1) {
		fr *= 10;
		num += 1;
	}
	if (num <= 4) {
		// 千
		newValue[1] = '千';
		newValue[0] = parseInt(value / 1000) + '';
	} else if (num <= 8) {
		// 万
		const text1 = parseInt(num - 4) / 3 > 1 ? '千万' : '万';
		const fm = '万' === text1 ? 10000 : 10000000;
		newValue[1] = text1;
		newValue[0] = value / fm + '';
	} else if (num <= 16) {
		// 亿
		let text1 = (num - 8) / 3 > 1 ? '千亿' : '亿';
		text1 = (num - 8) / 4 > 1 ? '万亿' : text1;
		text1 = (num - 8) / 7 > 1 ? '千万亿' : text1;
		// tslint:disable-next-line:no-shadowed-variable
		let fm = 1;
		if ('亿' === text1) {
			fm = 100000000;
		} else if ('千亿' === text1) {
			fm = 100000000000;
		} else if ('万亿' === text1) {
			fm = 1000000000000;
		} else if ('千万亿' === text1) {
			fm = 1000000000000000;
		}
		newValue[1] = text1;
		newValue[0] = parseInt(value / fm) + '';
	}
	if (value < 1000) {
		newValue[1] = '';
		newValue[0] = value + '';
	}
	let temp = Math.floor((newValue[0]) * 100) / 100
	return temp + newValue[1];
}

Vue.prototype.requestAsync = function(url, method, data) {
	return new Promise((resolve, reject) => {
		this.request(url, method, data, resolve, reject);
	});
}

Vue.prototype.loginAsync = function() {
	return new Promise((resolve, reject) => {
		uni.login({
			provider: 'weixin',
			success: (loginRes) => {
				resolve(loginRes);
			},
			fail: (error) => {
				reject(error);
			}
		});
	});
}


Vue.prototype.showErrorMessage = function(message) {
	this.$refs.uToast.show({
		type: 'error',
		icon: false,
		title: '错误',
		message: message,
		iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/error.png'
	});
}